package com.tzxs.pay.main.app;

import android.app.Activity;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.media.MediaPlayer;
import android.support.v7.app.AlertDialog;
import android.util.Log;

import com.tzxs.pay.R;
import com.tzxs.pay.main.bill.detail.CollAndInstallDetailActivity;
import com.tzxs.pay.main.onlineaut.OnlineAdActivity;
import com.tzxs.pay.util.websocket.model.WebSocketBean;

import java.lang.ref.WeakReference;
import java.util.Objects;

public class NoticeDialog {
    private static NoticeDialog instance;
    //这个要判断空

    private MediaPlayer player;
    private AlertDialog dialog;

    private NoticeDialog() {
    }

    public static NoticeDialog getInstance(TzApplication app) {
        if (instance == null) {
            //这里写错了
            instance = new NoticeDialog();
        }
        return instance;
    }

    public void showDialog(Activity activity,WebSocketBean.MsgBean msg) {
        //这里面创建你的dialog
        /*&& !activity.isFinishing()*/
        if (activity != null) {
            playAudio(activity);
//            Log.d("NoticeDialog", "showDialog: 当前activity:=== " + activity.getLocalClassName());
            AlertDialog.Builder builder = new AlertDialog.Builder(activity)
                    .setTitle(msg.getTitle())
                    .setMessage("订单号:" + msg.getOut_order_no() + "\n收款金额: " + msg.getAmount())
                    .setPositiveButton("确认(详情)", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            switch (msg.getType()) {
                                case "QRPAY":
                                    Intent intent = new Intent(activity, CollAndInstallDetailActivity.class);
                                    intent.putExtra("orderId", msg.getOut_order_no());
                                    activity.startActivity(intent);

                                    break;
                                case "AUTH":
                                    break;
                                case "SMSAUTH":
                                    Intent msgIntent = new Intent(activity, OnlineAdActivity.class);
                                    msgIntent.putExtra("orderId", msg.getOut_order_no());
                                    activity.startActivity(msgIntent);
                                    break;
                                case "ONLINEAUTH":
                                    break;
                                case "INVOICE":
                                    break;
                            }
                            dialog.dismiss();
                        }

                    })
                    .setNegativeButton("取消", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            dialog.dismiss();
                        }
                    });
            dialog = builder.show();
        }
    }


    /**
     * 取消Dialog
     */
    public void dismiss() {
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
        }
    }


    //播放音频
    private void playAudio(Activity activity) {
        try {
            if (player == null) {
                player = MediaPlayer.create(Objects.requireNonNull(activity).getApplicationContext(), R.raw.order_tips);
            }
            player.start();
            player.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mp) {
//                    player.stop();
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //停止播放声音
    public void stopVoice(MediaPlayer player) {
        if (null != player) {
            player.stop();
        }
    }
}
